function addTextDecoration(elem, prop){
	
		
		
		decorationString = $(elem).css("text-decoration");
		
		if(decorationString == "none"){
			decorationString = "";	
		}
		
		decorationString += " ";
		decorationString += prop;	
		
		$(elem).css("text-decoration", decorationString);
		
	}
	
	function removeTextDecoration(elem, prop){
		
		decorationString = $(elem).css("text-decoration");
		
		decorationString = decorationString.replace(prop, "");
		
		//alert(decorationString);
		
		decorationString = decorationString.replace(/\s/, "");
		
		//alert(decorationString);
		
		$(elem).css("text-decoration", decorationString);
			
	}
	
	// findCommonRange of the function that is used for text-decoration
	function findCommonRange(prop){
		sel = window.getSelection();
	
		range = sel.getRangeAt(0);
	
		initCommon = range.commonAncestorContainer;
		
		var comm;
		
		if(initCommon.nodeType == 3){
			initCommon = initCommon.parentNode;
		}
	
		if($(initCommon).css("text-decoration").indexOf(prop) != -1){
			comm = initCommon;
			return comm;
		}
	
		$(initCommon).parents().each(function(){
	
			if($(this).css("text-decoration").indexOf(prop) != -1){
				comm = this;
				return false;
			}
			
		});
		return comm;
	}

	
	function textDecorationOff(buttonName ,prop){

		
		sel = window.getSelection();
		
		range = sel.getRangeAt(0);
		
		if(range == "") return;
		
		common = findCommonRange(prop);
		
		spanFlag = 0;
		
		if(range.commonAncestorContainer.nodeType == 1 & range.toString().length == range.commonAncestorContainer.textContent.length  & range.commonAncestorContainer == common){

			spanFlag=1;
			
			removeTextDecoration(common, prop);
			
			$(common).children().each(function(){
				if($(this).css("text-decoration").indexOf(prop) != -1){
					removeTextDecoration(this, prop);	
				}
			});
			
			
		} else {
			
			
			range = window.getSelection().getRangeAt(0);
			
			rangeFront = document.createRange();
			
			rangeFront.setStartBefore(common)
			rangeFront.setEnd(range.startContainer, range.startOffset);
			
			haveSpan = 0;
			
			if(range.commonAncestorContainer.nodeType == 3 & range.commonAncestorContainer.parentNode!= common){
				haveSpan = 1;
				savedSpan = range.commonAncestorContainer.parentNode.cloneNode(false);
			}
					
			fragmentMiddle = range.extractContents();
			
			
			
			spanMiddle = document.createElement("span");
			
			if(haveSpan == 0){
				spanMiddle.appendChild(fragmentMiddle);	
			} else {
				savedSpan.appendChild(fragmentMiddle);
				spanMiddle.appendChild(savedSpan); 
			}
				
			fragment=rangeFront.extractContents();
			
			spanFront = document.createElement("span");
			
			spanFront.appendChild(fragment);
			
			rangeBack=document.createRange();
			
			strLen = range.toString().length
			
			rangeBack.setStart(range.endContainer, range.endOffset)
			rangeBack.setEndAfter(common);
			
			fragment2=rangeBack.extractContents();
			
			spanBack = document.createElement("span");
			
			spanBack.appendChild(fragment2)
			
			
			addTextDecoration(spanFront, prop);
			removeTextDecoration(spanMiddle, prop);
			addTextDecoration(spanBack, prop);
			
			common.appendChild(spanFront);
			common.appendChild(spanMiddle);
			common.appendChild(spanBack);
			
			removeTextDecoration(common, prop);
			
		}
		
		sel = window.getSelection();
		sel.removeAllRanges();
		
		newSelectRange = document.createRange();
		
		if(spanFlag == 0){
		
			newSelectRange.selectNodeContents(spanMiddle);
		
		} else {
			
			
			newSelectRange.selectNodeContents(common);	
		}
		
		sel.addRange(newSelectRange);
		
		butter.buttonState[buttonName] = 0;

	}
	
